<?php

namespace app\common;

use app\common\Common;
use think\facade\Cache;

//巨量数据查看
class HugeAmountClient
{
    protected $open_id; //账户open_id
    protected $secret; //密钥
    protected $url; //地址
    public function __construct()
    {
        $config = config('hugeamount');
        $this->open_id = $config['open_id'];
        $this->secret = $config['secret'];
        $this->url = $config['url'];
    }

    /**
     * 获取access_token
     *
     * @return string
     */
    public function getAccessToken()
    {
        $redis = Cache::store('redis')->handler();
        $redis->select(8);
        $token = $redis->get('hugeamount_' . $this->open_id);
        if ($token) {
            return $token;
        }
        $token_url = $this->url . '/account/access-token';
        //获取巨量token
        $arr = [
            'open_id' =>  $this->open_id,
            'secret' =>  $this->secret,
        ];
        $data = Common::curlPostRequest($token_url, $arr);

        $data = json_decode($data, true); //获取token
        if ($data['code'] == 200) {
            $token =  $data['data']['token_type'] . ' ' . $data['data']['access_token'];
            $redis->set('hugeamount_' . $this->open_id, $token, $data['data']['expires_in']);
            return $token;
        } else {
            return 0;
        }
    }
    /**
     * 获取账户
     *
     * @return array
     */
    public function getAccount()
    {
        $redis = Cache::store('redis')->handler();
        $redis->select(8);
        $list = $redis->get('hugeamount_accoun_list');
        if ($list) {
            return ['code' => 200, 'data' =>  json_decode($list, true)];
        }
        $token = $this->getAccessToken();
        if ($token === 0) {
            return ['code' => 0, 'msg' => 'token获取不到'];
        }
        $urls = $this->url . '/oceanengine/business/account';
        //获取
        $data = Common::curlRequest($urls, 'GET', '', ['authorization:' . $token]);

        $data = json_decode($data, true); //获取数据
        if ($data['code'] == 200) {
            $mlist = $data['data'];
            $redis->set('hugeamount_accoun_list', json_encode($mlist), 2 * 60 * 60);
            return ['code' => 200, 'data' => $mlist];
        } else {
            return  ['code' => 0, 'msg' => '未获取到数据'];
        }
    }
    /**
     * 获取巨量实时消耗
     *
     * @param [string] $key 账户key
     * @return array
     */
    public function getAdDataToday(string $key)
    {
        $redis = Cache::store('redis')->handler();
        $redis->select(8);
        $list = $redis->get('getAdDataToday_:' . $key);
        if ($list) {
            return ['code' => 200, 'data' =>  json_decode($list, true)];
        }
        $token = $this->getAccessToken();
        if ($token === 0) {
            return ['code' => 0, 'msg' => 'token获取不到'];
        }
        $urls = $this->url . '/oceanengine/business/ad-data-today?key=' . $key;
        //获取
        $data = Common::curlRequest($urls, 'GET', '', ['authorization:' . $token]);

        $data = json_decode($data, true); //获取数据
        if ($data['code'] == 200) {
            $mlist = $data['data'];
            $redis->set('getAdDataToday_' . $key, json_encode($mlist), 2 * 60);
            return ['code' => 200, 'data' => $mlist];
        } else {
            return  ['code' => 0, 'msg' => '未获取到数据'];
        }
    }
    /**
     * 获取巨量昨日财务数据
     *
     * @param string $key
     * @return array
     */
    public function getFinancesFailyDataYesterday(string $key)
    {
        $token = $this->getAccessToken();
        if ($token === 0) {
            return ['code' => 0, 'msg' => 'token获取不到'];
        }
        $urls = $this->url . '/oceanengine/business/finances-faily-data-yesterday?key=' . $key;

        //获取
        $data = Common::curlRequest($urls, 'GET', '', ['authorization:' . $token]);

        $data = json_decode($data, true); //获取数据
        if ($data['code'] == 200) {
            $mlist = $data['data'];
            return ['code' => 200, 'data' => $mlist];
        } else {
            return  ['code' => 0, 'msg' => '未获取到数据'];
        }
    }
    /**
     * 获取巨量分时段数据
     *
     * @param string $company_id //公司id
     * @param string $advertiser_id //账号id
     * @return array
     */
    public function getStatisticsChart(string $company_id, string $advertiser_id)
    {
        $token = $this->getAccessToken();
        if ($token === 0) {
            return ['code' => 0, 'msg' => 'token获取不到'];
        }
        $urls = $this->url . '/oceanengine/business/statistics-chart?company_id=' . $company_id . '&advertiser_id=' . $advertiser_id;
        //获取
        $data = Common::curlRequest($urls, 'GET', '', ['authorization:' . $token]);

        $data = json_decode($data, true); //获取数据
        if ($data['code'] == 200) {
            $mlist = $data['data'];
            return ['code' => 200, 'data' => $mlist];
        } else {
            return  ['code' => 0, 'msg' => '未获取到数据'];
        }
    }
}
